<!DOCTYPE html>
<html>

<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>广告管理</title>
    <!-- Tell the browser to be responsive to screen width -->
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <link rel="stylesheet" href="../plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/AdminLTE.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/skins/_all-skins.min.css">
    <link rel="stylesheet" href="../css/style.css">
    <script src="../plugins/jQuery/jquery-2.2.3.min.js"></script>
    <script src="../plugins/bootstrap/js/bootstrap.min.js"></script>
    <!--	<%&#45;&#45;bootstrap-fileinput&#45;&#45;%>-->
    <script type="text/javascript" src="/js/bootstrap-fileinput/js/fileinput.js"></script>
    <script type="text/javascript" src="/js/bootstrap-fileinput/js/locales/zh.js"></script>
    <link rel="stylesheet" type="text/css" href="/js/bootstrap-fileinput/css/fileinput.min.css"/>

</head>

<body class="hold-transition skin-red sidebar-mini">
<!-- .box-body -->

<div class="box-header with-border">
    <h3 class="box-title">广告管理</h3>
</div>

<div class="box-body">

    <!-- 数据表格 -->
    <div class="table-box">

        <!--工具栏-->
        <div class="pull-left">
            <div class="form-group form-inline">
                <div class="btn-group">
                    <button type="button" class="btn btn-default" title="新建" data-toggle="modal"
                            data-target="#add"><i class="fa fa-file-o"></i> 新建
                    </button>
                    <button type="button" class="btn btn-default" title="删除" onclick="deleteInfo()"><i class="fa fa-trash-o"></i> 删除</button>
                    <button type="button" class="btn btn-default" title="开启" onclick='confirm("你确认要开启吗？")'><i
                            class="fa fa-check"></i> 开启
                    </button>
                    <button type="button" class="btn btn-default" title="屏蔽" onclick='confirm("你确认要屏蔽吗？")'><i
                            class="fa fa-ban"></i> 屏蔽
                    </button>
                    <button type="button" class="btn btn-default" title="刷新" onclick="window.location.reload();"><i
                            class="fa fa-refresh"></i> 刷新
                    </button>
                </div>
            </div>
        </div>
        <div class="box-tools pull-right">
            <div class="has-feedback">

            </div>
        </div>
        <!--工具栏/-->

        <!--数据列表-->
        <table id="dataList" class="table table-bordered table-striped table-hover dataTable">
            <thead>
            <tr>
                <th class="" style="padding-right:0px">
                    <input id="selall" type="checkbox" class="icheckbox_square-blue">
                </th>
                <th class="sorting_asc">广告ID</th>
                <th class="sorting">分类ID</th>
                <th class="sorting">标题</th>
                <th class="sorting">URL</th>
                <th class="sorting">图片</th>
                <th class="sorting">排序</th>
                <th class="sorting">是否有效</th>
                <th class="text-center">操作</th>
            </tr>
            </thead>
            <script>
                $(function () {
                    show();
                })

                function show() {
                    $.ajax({
                        url: "/content/getContent",
                        data: {name: $("#name").val()},
                        type: "get", //请求方式 get post 默认get
                        dataType: "json", //返回值类型 html json text xml
                        async: true,//true代表异步请求 false代表同步请求
                        success: function (result) {//成功回调函数
                            var page = result.data;
                            var div = "";
                            for (var i = 0; i < page.length; i++) {
                                div += '<tr><td><input type="checkbox" name="check" value="' + page[i].id + '"></td>';
                                div += '<td>' + page[i].id + '</td>';
                                div += '<td>' + page[i].categoryId + '</td>';
                                div += '<td>' + page[i].title + '</td>';
                                div += '<td>' + page[i].url + '</td>';
                                div += '<td><img alt="" src="' + page[i].pic + '" width="100px" height="50px"></td>';
                                div += '<td>' + page[i].sortOrder + '</td>';
                                if (page[i].status == 1) {
                                    div += '<td>有效</td>';
                                } else {
                                    div += '<td>无效</td>';
                                }
                                div += '<td className="text-center">';
                                div += '<button type="button" className="btn bg-olive btn-xs"data-toggle="modal" data-target="#editModal" onclick="toUpdate(' + page[i].id + ')">修改</button></td></tr>';
                            }
                            $("#html").html(div);
                        },
                        error: function (i) {//发生错误回调函数
                            alert("异常错误")
                        }

                    })
                }
                function deleteInfo(){
                    var rows = $("[name=check]:checked");
                    var ids = [];
                    $(rows).each(function (i, e) {
                        ids[i] = e.value;
                    })
                    if (ids.length == 0) {
                        toastr.success("请选择要删除的数据");
                        return;
                    }
                    $.ajax({
                        url: "/content/deleteInfo?ids=" + ids,
                        type: "post", //请求方式 get post 默认get
                        dataType: "json", //返回值类型 html json text xml
                        async: true,//true代表异步请求 false代表同步请求
                        success: function (result) {//成功回调函数
                            if (result.code == 10000) {
                                toastr.success("删除成功");
                                window.location.reload();
                            } else {
                                toastr.error("删除失败");
                                window.location.reload();
                            }

                        },
                        error: function (i) {//发生错误回调函数
                            alert("异常错误")
                        }
                    })
                }
            </script>
            <tbody id="html">
            </tbody>
        </table>
        <!--数据列表/-->
    </div>
    <!-- 数据表格 /-->
</div>
<!-- /.box-body -->

<!-- 编辑窗口 -->
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h3 id="myModalLabel">广告编辑</h3>
            </div>
            <div class="modal-body">
                <form id="form2">
                    <table class="table table-bordered table-striped" width="800px">
                        <tr>
                            <td>广告分类</td>
                            <input type="hidden" name="id" id="DD">
                            <td>
                                <select class="form-control" name="categoryId" id="cid1">
                                    <option value="-1" disabled hidden selected >--请选择--</option>
                                    <option th:each="categroy : ${list}" th:value="${categroy.id}">
                                        [[${categroy.name}]]
                                    </option>
                                </select>
                            </td>
                        </tr>
                        <tr>
                            <td>标题</td>
                            <td><input class="form-control" name="title" id="title1" placeholder="标题"></td>
                        </tr>
                        <tr>
                            <td>URL</td>
                            <td><input class="form-control" name="url" id="url1" placeholder="URL"></td>
                        </tr>
                        <tr>
                            <td>排序</td>
                            <td><input class="form-control" name="sortOrder" id="sortOrder1" placeholder="排序"></td>
                        </tr>
                        <tr>
                            <td>广告图片</td>
                            <td>
                                <table>
                                    <tr>
                                        <td>
                                            <input type="file" class="form-control" name="myFile" id="myFile1">
                                        </td>
                                        <td>
                                            <input type="hidden" id="photo1" name="pic">
                                        </td>
                                    </tr>
                                </table>
                            </td>
                        </tr>
                        <tr>
                            <td>是否有效</td>
                            <td>
                                <input type="checkbox" class="icheckbox_square-blue" name="status" id="status1" value="1">
                            </td>
                        </tr>
                    </table>
                </form>
            </div>
            <div class="modal-footer">
                <button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="update()">保存</button>
                <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
            </div>
        </div>
    </div>
</div>
<script>
    function toUpdate(id) {
        $.ajax({
            url: "/content/toUpdate",
            data: {id: id},
            type: "get", //请求方式 get post 默认get
            dataType: "json", //返回值类型 html json text xml
            async: true,//true代表异步请求 false代表同步请求
            success: function (result) {//成功回调函数
                var page = result.data;
                $("#DD").val(page.id);
                $("#cid1").val(page.categoryId);
                $("#title1").val(page.title);
                $("#url1").val(page.url);
                $("#sortOrder1").val(page.sortOrder);
                $("#photo1").val(page.pic);
                $("#status1").attr("checked", "checked");
                $("#myFile1").fileinput({
                    language: 'zh',
                    uploadUrl: "/test/upload",
                    showCaption: true,
                    showUpload: true,
                    showRemove: true,
                    showClose: true,
                    layoutTemplates: {
                        actionDelete: ''
                    },
                    initialPreview: ['<img src="' + page.pic + '" width="200px" height="150px">'],
                    browseClass: 'btn btn-primary',
                    allowedFileExtensions: ['jpg', 'png', 'gif', 'txt'],//限制文件上传类型
                    maxFileCount: 1,//最大上传数量
                    maxFileSize: 2048,//设置文件上传大小 单位kb
                    enctype: "multipart/form-data"
                });
                $("#myFile1").on('fileuploaded', function (event, data, previewId, index) {

                    if (data.response != null) {
                        $("#photo1").val(data.response.data);

                    }
                })
            },
            error: function (i) {//发生错误回调函数
                alert("异常错误")
            }

        })
    }
    function update(){
        $.ajax({
            url: "/content/update",
            data:$("#form2").serialize() + "&id=" + $("#DD").val(),
            type: "get", //请求方式 get post 默认get
            dataType: "json", //返回值类型 html json text xml
            async: true,//true代表异步请求 false代表同步请求
            success: function (result) {//成功回调函数
                if (result.code == 10000){
                    alert("修改成功")
                    window.location.reload();
                }else {
                    alert("失败")
                }
            },
            error: function (i) {//发生错误回调函数
                alert("异常错误")
            }

        })
    }
</script>

<!-- 编辑窗口 -->
<div class="modal fade" id="add" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h3 id="myModalLabel1">广告编辑</h3>
            </div>
            <div class="modal-body">
                <form id="form1">
                    <table class="table table-bordered table-striped" width="800px">
                        <tr>
                            <td>广告分类</td>
                            <td>
                                <select class="form-control" name="categoryId" id="cid">
                                    <option value="-1" disabled hidden selected >--请选择--</option>
                                    <option th:each="categroy : ${list}" th:value="${categroy.id}">
                                        [[${categroy.name}]]
                                    </option>
                                </select>
                            </td>
                        </tr>
                        <tr>
                            <td>标题</td>
                            <td><input class="form-control" name="title" id="title" placeholder="标题"></td>
                        </tr>
                        <tr>
                            <td>URL</td>
                            <td><input class="form-control" name="url" id="url" placeholder="URL"></td>
                        </tr>
                        <tr>
                            <td>排序</td>
                            <td><input class="form-control" name="sortOrder" id="sortOrder" placeholder="排序"></td>
                        </tr>
                        <tr>
                            <td>广告图片</td>
                            <td>
                                <table>
                                    <tr>
                                        <td>
                                            <input type="file" class="form-control" name="myFile" id="myFile">
                                        </td>
                                        <td>
                                            <input type="hidden" id="photo" name="pic">
                                        </td>
                                    </tr>
                                </table>
                            </td>
                        </tr>
                        <tr>
                            <td>是否有效</td>
                            <td>
                                <input type="checkbox" class="icheckbox_square-blue" value="1" name="status"
                                       id="status" checked="checked">
                            </td>
                        </tr>
                    </table>
                </form>
            </div>
            <div class="modal-footer">
                <button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="add()">保存</button>
                <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
            </div>
        </div>
    </div>
</div>

</body>

</html>
<script>
    $("#myFile").fileinput({
        language: 'zh',
        uploadUrl: "/test/upload",
        showCaption: true,
        showUpload: true,
        showRemove: true,
        showClose: true,
        layoutTemplates: {
            actionDelete: ''
        },
        browseClass: 'btn btn-primary',
        allowedFileExtensions: ['jpg', 'png', 'gif', 'txt'],//限制文件上传类型
        maxFileCount: 1,//最大上传数量
        maxFileSize: 2048,//设置文件上传大小 单位kb
        enctype: "multipart/form-data"
    });
    $("#myFile").on('fileuploaded', function (event, data, previewId, index) {

        if (data.response != null) {
            $("#photo").val(data.response.data);

        }
    })

    // function add() {
    //     var color = $("#color").val();
    //     var url = $("#photo").val();
    //     $("#tbody").append(
    //         '<tr><td>' + color + '</td> <td><img alt="" height="100px" src="' + url + '" width="100px"> </td> <td> <button className="btn btn-default" onclick="deleteOneTr1(this)" title="删除"type="button"><iclassName="fa fa-trash-o"></i> 删除 </button></td></tr>'
    //     )
    // }
    function add() {
        $.ajax({
            url: "/content/addInfo",
            data: $("#form1").serialize(),
            type: "get", //请求方式 get post 默认get
            dataType: "json", //返回值类型 html json text xml
            async: true,//true代表异步请求 false代表同步请求
            success: function (result) {//成功回调函数
                if (result.code == 10000) {
                    alert("增加成功")
                    window.location.reload();
                }
            },
            error: function (i) {//发生错误回调函数
                alert("异常错误")
            }

        })
    }

</script>